﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace GDFlacTool.Forms
{
    public partial class FormAbbreviations : Form
    {
        private readonly Dictionary<string, string> dictionary = new Dictionary<string, string>(); 

        public FormAbbreviations()
        {
            InitializeComponent();

            Init();

            cboAbbreviation.SelectedIndex = 0;
        }

        private void Init()
        {
            StringCollection items = Properties.AbbrivationSettings.Default.Abbrivations;
            var separators = new[] {";"};

            if(items.Count > 0)
            {
                dictionary.Add("", "");

                foreach (var item in items)
                {
                    string[] values = item.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if(values.Length == 2)

                    dictionary.Add(values[0], values[1]);
                }

                cboAbbreviation.DataSource = new BindingSource(dictionary, null);
                cboAbbreviation.ValueMember = "Value";
                cboAbbreviation.DisplayMember = "Key";
            }
            else
            {
                btnCopyToClipboard.Enabled = false;
                cboAbbreviation.Enabled = false;
            }
        }

        private void cboAbbreviation_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTranslation.Text = cboAbbreviation.SelectedValue.ToString();
        }

        private void FormAbbreviations_Load(object sender, EventArgs e)
        {
            cboAbbreviation.SelectedIndex = 0;

            txtTranslation.Text = string.Empty;
        }

        private void txtTranslation_TextChanged(object sender, EventArgs e)
        {
            btnCopyToClipboard.Enabled = !string.IsNullOrEmpty(txtTranslation.Text);
        }

        private void btnCopyToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(txtTranslation.Text.TrimEnd());
        }
    }
}
